//
//  TKYRemarkView.m
//  ProgressReportProject
//
//  Created by apple on 2018/4/23.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYRemarkView.h"
@interface TKYRemarkView()

@property (nonatomic, strong)UILabel *stirngLenghLabel;
@end
@implementation TKYRemarkView

//进度备注的view
- (instancetype)initWithFrame:(CGRect)frame titleLabelText:(NSString *)titleLabelTex remarkText:(NSString *)remarkText{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self initWithTitleLabelText:titleLabelTex remarkText:remarkText];
        
    }
    return self;
}

- (void)initWithTitleLabelText:(NSString *)titleLabelTex remarkText:(NSString *)remarkText{
    
    
   // UIView *superView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.kx_width, self.kx_height)];
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidthScale *20, 10, kWidthScale *70, kHeightScale *20)];
    titleLabel.text = titleLabelTex;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [titleLabel sizeToFit];
    
    [self addSubview:titleLabel];
   
    
    self.remarkTextView = [[UITextView alloc]initWithFrame:CGRectMake(titleLabel.kx_right +10, 5, kScreenWidth - kWidthScale *110 , kHeightScale *100)];
    //self.remarkTextView.backgroundColor = [UIColor redColor];
    self.remarkTextView.delegate = self;
    self.remarkTextView.text = remarkText;
    //self.remarkTextView.textColor = [UIColor blackColor];
    self.remarkTextView.font = [UIFont systemFontOfSize:14];
   // [self.remarkTextView tky_cornerWithRadius:5 borderColor:kRGBColor(204, 204,204, 1) borderWidth:1];

    self.remarkTextView.textAlignment = NSTextAlignmentLeft;
//    self.remarkTextView.text = @"请输入备注信息";
//    self.remarkTextView.textColor = [UIColor grayColor];

    [self addSubview:self.remarkTextView];
    
    self.stirngLenghLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.remarkTextView.kx_width - 50, self.remarkTextView.kx_height - 20, 50, 20)];
    self.stirngLenghLabel.text = @"0/100";
    self.stirngLenghLabel.font = [UIFont systemFontOfSize:14];
    [self.remarkTextView addSubview:self.stirngLenghLabel];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.kx_height +5, kScreenWidth, 1)] ;
    lineLabel.backgroundColor = kUIColorFromRGB(0XE5E5E5, 1);
    [self addSubview:lineLabel];
}

//计算文本的宽度
- (CGFloat )sizeWidth:(NSString *)text{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGFloat widthSize = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.width;
    
    return widthSize;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (textView == self.remarkTextView) {
        self.remarkTextBlock(text);
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    
    if (textView == self.remarkTextView) {
        self.remarkTextBlock(textView.text);
    }
}

//正在改变
- (void)textViewDidChange:(UITextView *)textView
{
    //实时显示字数
    self.stirngLenghLabel.text = [NSString stringWithFormat:@"%lu/100", (unsigned long)textView.text.length];
    
    //字数限制操作
    if (textView.text.length >= 100) {
        KALERTSHOW(@"提示", @"只能输入100个文字", @"确定");
        textView.text = [textView.text substringToIndex:100];
        self.stirngLenghLabel.text = @"100/100";
    }
    
    if (self.remarkTextBlock) {
        self.remarkTextBlock(textView.text);
    }
}
@end
